package com.lh.web.controller.shop;

import com.lh.common.entity.admin.BannerShop;
import com.lh.common.entity.admin.Shop;
import com.lh.common.enums.IF;
import com.lh.common.enums.IsDel;
import com.lh.common.enums.ShopTypeEnum;
import com.lh.common.enums.UpperLowerShelfStatus;
import com.lh.common.utils.*;
import com.lh.web.controller.AbstractController;
import com.lh.web.service.serviceAdmin.shop.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FH
 * @ClassName: ShopAction
 * @Description: (商品管理)
 * @date: 2020-05-12 14:55
 */
@Api(value="ShopAction",tags={"商品管理"})
@RestController
@RequestMapping("shop")
public class ShopAction extends AbstractController {

    @Autowired
    private ShopService shopService;

    /**
     * 查询商品管理
     */
    @ApiOperation(value = "查询商品管理")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "shopName", value = "商品名称", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "shopId", value = "商品ID", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "shopStatic", value = "商品状态", required = false, dataTypeClass= UpperLowerShelfStatus.class),
            @ApiImplicitParam(paramType="query", name = "shopType", value = "商品类型", required = false, dataTypeClass= ShopTypeEnum.class),
            @ApiImplicitParam(paramType="query", name = "sortId", value = "商品分类", required = false, dataTypeClass= Integer.class)
    })
    @GetMapping("/getShopList")
    @RequiresPermissions("shop:getShopList")
    public Result<Shop> getShopList(String shopName,String shopId,UpperLowerShelfStatus shopStatic, ShopTypeEnum shopType,String sortId,Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("shopName", shopName);
        params.put("shopId", shopId);
        params.put("sortName", sortId);
        if(shopStatic != null) {
            params.put("shopStatic", shopStatic.getValue());
        }
        if(shopType != null) {
            params.put("shopType", shopType.getValue());
        }
        PageUtils<Shop> shopMyPage = shopService.queryShopList(params,pageNum,pageSize);
        return Result.ok(shopMyPage);
    }

    /**
     * 关联商品查询
     */
    @ApiOperation(value = "关联商品查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "shopName", value = "商品名称", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "shopType", value = "商品分类", required = true, dataTypeClass= ShopTypeEnum.class)
    })
    @GetMapping("/getBannerShopList")
    @RequiresPermissions("shop:getBannerShopList")
    public Result<BannerShop> getBannerShopList(String shopName,ShopTypeEnum shopType,Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("shopName", shopName);
        if(shopType != null) {
            params.put("shopType", shopType.getValue());
        }else {
            return Result.error("商品分类为空!");
        }
        PageUtils<BannerShop> shopMyPage = shopService.getBannerShopList(params,pageNum,pageSize);
        return Result.ok(shopMyPage);
    }

    /**
     * 商品详情页面
     */
    @ApiOperation(value = "商品详情页面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "shopId", value = "商品id", required = true, dataTypeClass= Long.class)
    })
    @GetMapping("/getShopDetail")
    @RequiresPermissions("shop:getShopDetail")
    public Result<Shop> getShopDetail(Long shopId){
        AssertUtil.assertNotNull(shopId, Constant.RESULT.FI1000, "商品id为空!");
        Shop shop = shopService.getShopDetail(shopId);
        return Result.ok(shop);
    }

    /**
     * 新增商品
     */
    @ApiOperation(value = "新增/修改商品")
    @PostMapping("/addShop")
    @RequiresPermissions("shop:addShop")
    public Result<Shop> setShop(@RequestBody Shop shop) {
        try {
            AssertUtil.assertNotNull(shop.getName(), Constant.RESULT.FI1000, "商品名称为空!");
            AssertUtil.assertNotNull(shop.getPrice(), Constant.RESULT.FI1000, "商品价格为空!");
            AssertUtil.assertNotNull(shop.getSingleprice(), Constant.RESULT.FI1000, "成本价为空!");
            AssertUtil.assertNotNull(shop.getProperty(), Constant.RESULT.FI1000, "商品属性为空!");
            AssertUtil.assertNotTrue(shop.getPrice().equals(BigDecimal.ZERO),Constant.RESULT.FI1000, "商品价格不能为ZERO!");
            AssertUtil.assertNotTrue(shop.getSingleprice().equals(BigDecimal.ZERO),Constant.RESULT.FI1000, "成本价不能为ZERO!");
            shop.setUpdateTime(new Date());
            if(shop.getSid() == null) {
                Shop categoryName = shopService.getCategoryName(shop.getName());
                if(categoryName != null){
                    return Result.error("商品名称已存在!");
                }
                shop.setStatus(UpperLowerShelfStatus.LOWERSHELF);
                shopService.saveShop(shop);
                if(shop.getType().equals(ShopTypeEnum.SHOP)){
                    shopService.addShopSortRef(shop.getSid(),shop.getSortIds());
                }
                return Result.ok(shop);
            }
            AssertUtil.assertNotNull(shop.getSid(), Constant.RESULT.FI1000, "商品ID为空!");
            if(shop.getIsFreight().equals(IF.YES)){
                shop.setFirstFreight(BigDecimal.valueOf(0));
                shop.setNoFirstFreight(BigDecimal.valueOf(0));
            }
            shopService.updateById(shop);
            if(shop.getType().equals(ShopTypeEnum.SHOP)){
                shopService.addShopSortRef(shop.getSid(),shop.getSortIds());
            }
            return Result.ok(shop);
        } catch (Exception e) {
            return Result.error();
        }
    }

    @ApiOperation(value = "shop上下架/删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "sid", value = "sid", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "status", value = "状态", required = false, dataTypeClass = UpperLowerShelfStatus.class),
            @ApiImplicitParam(paramType="query",name = "isRecommend", value = "推荐", required = false, dataTypeClass = IF.class)
    })
    @ResponseBody
    @PostMapping("/updateShopStatus")
    @RequiresPermissions("shop:updateShopStatus")
    public Result updateNoticeStatus(@RequestBody Shop shop) {
        try {
            AssertUtil.assertNotNull(shop.getSid(), Constant.RESULT.FI1000, "商品ID为空!");
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("sid",shop.getSid());
            if(shop.getIsRecommend() != null){
                params.put("isRecommend",shop.getIsRecommend().getValue());
            }
            if(shop.getStatus()!= null) {
                if(shop.getStatus().equals(UpperLowerShelfStatus.LOWERSHELF)){
                    int i = shopService.queryActivityShop(shop.getSid());
                    if(i>0){
                        return Result.error("该商品被活动引用,不能执行下架操作!");
                    }
                    int j = shopService.queryShop(shop.getSid());
                    if(j>0){
                        return Result.error("该商品被banner引用,不能执行下架操作!");
                    }
                }else {
                    params.put("isRecommend",IF.NO.getValue());
                }
                params.put("status",shop.getStatus().getValue());
            }
            shopService.updateShopStatus(params);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("修改shop上下架/删除异常!");
        }
    }

    /**
     * 导出商品
     */
    @ApiOperation(value = "导出商品")
    @GetMapping("/export")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "shopName", value = "商品名称", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "shopStatic", value = "商品状态", required = false, dataTypeClass= UpperLowerShelfStatus.class),
            @ApiImplicitParam(paramType="query", name = "shopType", value = "商品分类", required = true, dataTypeClass= ShopTypeEnum.class),
    })
    @RequiresPermissions("shop:export")
    public void export(String shopName,UpperLowerShelfStatus shopStatic, ShopTypeEnum shopType, HttpServletResponse response) throws Exception {
        AssertUtil.assertNotNull(shopType, Constant.RESULT.FI1000, "未选择商品分类!");
        Map<String, Object> params = new HashMap<String, Object>();
        if(shopType != null) {
            params.put("shopType", shopType.getValue());
        }
        if(shopStatic != null) {
            params.put("shopStatic", shopStatic.getValue());
        }
        params.put("shopName",shopName);
        PageUtils page = shopService.queryShopList(params,0,Integer.MAX_VALUE);
        ExcelUtilJXL.exportExcelToBrowserForJSP(Shop.class, page.getList(),"商品信息.xls",response);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "sid", value = "商品ID", required = true)
    })
    @GetMapping("/deleteById")
    @ResponseBody
    @RequiresPermissions("shop:deleteById")
    public Result del(Long sid){
        try {
            shopService.deleteShop(sid);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("删除商品图异常!");
        }
    }
}
